<script language="javascript">
 var updateTimeOut;
 var currentURL='#';
 var currentID=0;

 function addURL() {
  var newURL=prompt("Please enter an URL","http://");
  if (newURL!=null && newURL!="" && newURL!='http://') {
   var url="?";
   var channel_id=$('#channel_id').val();
   url+='&ajax=1&op=add&channel_id='+channel_id+'&url='+encodeURIComponent(newURL);
   $.ajax({
    url: url
    }).done(function(data) { 
     updateURLs();
   });
  }
  return false;
 }

 function addToFavorites(url) {
  return false;
 }

 function removeFromFavorites(url) {
  return false;
 }

 function deleteURL(id) {

   if (!confirm('<#LANG_ARE_YOU_SURE#>')) return false;

   var url="?";
   var channel_id=$('#channel_id').val();
   url+='&ajax=1&op=delete&channel_id='+channel_id+'&id='+id;
   $.ajax({
    url: url
    }).done(function(data) { 
     updateURLs();
   });
  return false;
 }

 function setCurrentURL(url, title, id) {
  currentURL=url;
  currentID=id;
  var resHTML='';
  resHTML+='<b>'+title+'</b>'+' [<a href="'+url+'" target=_blank><#LANG_OPEN#></a>]';
  resHTML+=' [<a href="#" onClick="return deleteURL('+id+')">X</a>]';
  $('#currentURL').html(resHTML);
  $('#readit_frame').attr('src',url);
  return false;
 }

 function autoUpdateChanged() {
  clearTimeout(updateTimeOut);
  if ($('#auto_update').attr('checked')) {
   updateURLs();
  }
  return false;
 }

 function updateURLs() {
  clearTimeout(updateTimeOut);
  var url="?";
  var channel_id=$('#channel_id').val();
  url+='&ajax=1&op=geturls&channel_id='+channel_id;
  $.ajax({
   url: url
  }).done(function(data) { 
   var obj=jQuery.parseJSON(data);
   if (obj.URLS.length>0) {
    var urls=obj.URLS;
    if (urls[0].URL!=currentURL) {
     setCurrentURL(urls[0].URL, urls[0].TITLE, urls[0].ID);
    }
    $('#linksList').html();
    var resHTML='<table width="100%" border="0">';
    for(var i=0;i<urls.length;i++) {
     resHTML+='<tr>';
     resHTML+='<td valign="top">';
     resHTML+='<a href="'+urls[i].URL+'" target="readit_frame" onClick="$(\'#auto_update\').attr(\'checked\', false);autoUpdateChanged();return setCurrentURL(\''+urls[i].URL+'\', \''+urls[i].TITLE_SAFE+'\', '+urls[i].ID+')">'+urls[i].TITLE+'</a>';
     resHTML+='</td>';
     resHTML+='</tr>';
    }
    resHTML+='</table>';
    $('#linksList').html(resHTML);
   } else {
    $('#linksList').html('...');
   }
  }
  );
  if ($('#auto_update').attr('checked')) {
   updateTimeOut=setTimeout('updateURLs();', 3000);
  }
  return false;
 }

 function showHideSideURLs() {
  $('#channelSelTD').toggle();
  $('#urlsSelTD').toggle();

  if ( $("#channelSelTD").is(":visible") ) {
   $("#channelsToggleIcon").attr("src","/img/icons/application-side-contract-icon.png");
  } else { 
   $("#channelsToggleIcon").attr("src","/img/icons/application-side-expand-icon.png");
  }

  return false;
 }
 $(document).ready(function() {
  iFrameHeight=$(document).height()-100;
  $('#readit_frame').height(iFrameHeight);
  updateURLs();
});
</script>

<table width="100%" border="0">
 <tr>
  <td valign="top" width="300" id="channelSelTD" style="display:none"><#LANG_CHANNEL#>:
  <select name="channel_id" id="channel_id" onChange="updateURLs();">
   <option value="0">[<#LANG_ALL#>]
   <!--#
   <option value="favorite">[Favorites]
   #-->
   [#begin CHANNELS#]
   <option value="[#ID#]">[#TITLE#]
   [#end CHANNELS#]
  </select>
  <label><input type="checkbox" name="auto_update" value="1" id="auto_update" checked onChange="autoUpdateChanged();"> <#LANG_AUTO_UPDATE#></label>
  </td>
  <td valign="top">
   <a href="#" onClick="return showHideSideURLs();"><img src="/img/icons/application-side-expand-icon.png" id="channelsToggleIcon" border="0"></a>
   <span id="currentURL">
    ...
   </span>
  </td>
 </tr>
 <tr>
  <td valign="top" width="300" id="urlsSelTD" style="display:none">
   <div id="linksList">
    links
   </div>
   &nbsp;
   <div>
    [<a href="#" onClick="return addURL();"><#LANG_ADD_URL#></a>]
    [<a href="javascript:(function(){var host='<#COMPUTER_NAME#>';var title=prompt('<#LANG_TITLE#>:', document.title);if (title!='' && title!=null) {location.href='http://'+host+'/popup/app_readit.html?ajax=1&op=add&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(title);}}());"><#LANG_SEND_TO_HOME#></a>]
   </div>
  </td>
  <td valign="top"><iframe src="#" width="100%" id="readit_frame" name="readit_frame" height="100%" frameborder=1 allowTransparency="false" style="background-color:white;"></iframe></td>
 </tr>
</table>


